<style lang="scss">
  @import "~@/styles/base";

  .page {
    height: 100vh;
    .page-bind {
      background-color: #fff;
      border-radius: rpx(10);
      min-height: 100%;
      padding-top: rpx(55);
      .icon-wifi {
        display: block;
        width: rpx(624);
        height: rpx(418);
        margin-left: auto;
        margin-right: auto;
      }
      .tip {
        margin-top: rpx(20);
        margin-bottom: rpx(86);
        font-size: rpx(34);
        color: $black;
        text-align: center;
      }
      .title {
        padding-top: rpx(165);
        padding-bottom: rpx(30);
        color: $light-black;
        font-size: rpx(24);
        line-height: rpx(40);
        text-align: center;
      }
      .wifi-name {
        margin-bottom: rpx(155);
        color: $black;
        font-size: rpx(28);
        line-height: rpx(40);
        text-align: center;
      }
    }
  }
</style>
<template>
  <div class="page">
    <div class="page-bind">
      <div class="tip">选择设备Wi-Fi</div>
      <img class="icon-wifi" src="/static/img/bind/tutorial2_img.png">
      <div class="title">当前连接</div>
      <div class="wifi-name">{{SSID}}</div>
      <div class="btn" @click="getWifiList">去设置WIFI</div>
    </div>
  </div>
</template>

<script>
  import wx from "utils/wx";
  import {WIFI_CHECK_REG} from 'config';

  export default {
    data() {
      return {
        isIos: /iOS/.test(App.systemInfo.system),
        SSID: "",
        complete: false
      };
    },
    components: {},
    methods: {
      getWifiList(){
        wx.getWifiList();
      },
      toNext() {
        const pages = getCurrentPages();
        if (pages.pop().route === "bind/pages/step3/main" && this.complete) {
          this.clear();
          wx.redirectTo({ url: "/bind/pages/step4/main" });
          return false;
        }
      },
      async getConnectedWifi() {
        console.info("jumped:" + this.jumped);
        if (this.jumped) {
          return false;
        }
        this.jumped = true;
        let connectResult = await wx.getConnectedWifi();
        this.SSID = connectResult.wifi.SSID;
        console.info("SSID:" + this.SSID);
        App.deviceiSSID = this.SSID;
        console.log("connectResult:" + JSON.stringify(connectResult));
        this.complete = WIFI_CHECK_REG.test(this.SSID);
        if (this.complete && this.jumped) {
          this.toNext();
          return false;
        }
        this.jumped = false;
      },
      clear() {
        this.complete = false;
        this.jumped = false;
        clearInterval(this.wifiTask);
        this.SSID = "";
        this.connectWifi = false;
      }
    },
    onHide() {
      this.clear();
    },
    async onShow() {
      if (!Store.getters.is_login) {
        await Store.dispatch('login');
      }
      await wx.startWifi();
      await this.getConnectedWifi();
      wx.onWifiConnected(result => {
        this.SSID = result.wifi.SSID;
        console.info("SSID:" + this.SSID);
        this.complete = WIFI_CHECK_REG.test(this.SSID);
        console.log("connectResult:" + JSON.stringify(result));
        if (this.complete && this.jumped) {
          App.deviceiSSID = result.wifi.SSID;
          this.toNext();
          return false;
        }
      });
      if (!this.isIos) {
        wx.onGetWifiList(result => {
          if (result.wifiList) {
            result.wifiList.forEach(wifi => {
              if (this.connectWifi) {
                return false;
              }
              if (WIFI_CHECK_REG.test(wifi.SSID)) {
                clearInterval(this.wifiTask);
                this.connectWifi = true;
                App.deviceiSSID = wifi.SSID;
                wx.connectWifi({
                  SSID: wifi.SSID,
                  password: "",
                  success: () => {
                    this.connectWifi = false;
                  },
                  fail: () => {
                    this.connectWifi = false;
                  },
                  complete: () => {
                    this.connectWifi = false;
                  }
                });
              }
            });
          }
        });
        wx.getWifiList();
        this.wifiTask = setInterval(async () => {
          wx.getWifiList();
        }, 2000);
      }
    }
  };
</script>
